<template>
    <view class="body-view">
        <!--导航和状态栏-->
        <view class="state-navigation">
            <view class="slim_header_navigation d-flex a-center j-center">
                <view class="left_box" @tap="goBack">
                    <image src="/static/center/back.png" class="back"></image>
                </view>
                <view class="title">{{ title[type] }}打卡</view>
                <view class="right_box">
					<view @tap="showDateTable" class="">
						<view style="text-align: center;font-size: 26rpx;font-weight: 400;">{{moment(selectDate).format("M月D日")}}</view>
						<view style="text-align: center;font-size: 26rpx;font-weight: 400;">{{moment(selectDate).locale('zh-cn').format('dddd')}}</view>
					</view>
					<!--
                    <view
                        v-if="isAdmin"
                        class="btn-text"
                        @tap="showInitiateClockin"
                        >发起打卡</view
                    >
					-->
                    <!-- <image src="/static/center/dian.png" class="dian"></image> -->
                </view>
            </view>
        </view>
        <!-- 顶部位置占位 -->
        <view style="height: calc(100rpx + var(--status-bar-height))"></view>
        <view class="content">
            <view class="content-title d-flex a-center j-center">
                <text class="title-text">{{ title[type] }}已打卡：</text>
                <text class="title-number"
                    >{{
                        total
                    }}/{{pageData.filter((item:any) => isSelect(item)).length}}</text
                >
            </view>
            <scroll-view scroll-y="true" class="scroll-member">
                <view
                    @tap="showPopupCount(item, index)"
                    class="member-row d-flex a-center j-between"
                    v-for="(item,index) in pageData.filter((item:any) => isSelect(item))"
                >
                    <view class="row-divider" v-if="index > 0"></view>
                    <view class="row-left d-flex a-center">
                        <userhead
                            width="100"
                            height="100"
                            :saveKey="item.userId"
                            :isVip="item.isVip"
                            :src="item.userPhoto"
                        />
                        <view class="row-left-info">
                            <view class="left-info-top d-flex a-center">
                                <view style="padding-top: 5rpx">
                                    <sexage :sex="item.sex" :age="item.age" />
                                </view>
                                <view
                                    class="userName"
                                    :style="{
                                        color:
                                            item.isVip === 2
                                                ? '#F19945'
                                                : '#333333',
                                    }"
                                    >{{ item.userName }}</view
                                >
                            </view>
                            <view class="left-info-below d-flex a-center">
                                <view class="info-below-text">{{
                                    clockinDate(item)
                                }}</view>
                                <view
                                    class="info-below-text"
                                    style="padding-left: 35rpx"
                                    >已减重:{{ item.yjz }}kg</view
                                >
                            </view>
                        </view>
                    </view>
                    <view class="row-right">
                        <view
                            v-if="isClockin(item)"
                            class="d-flex d-c a-center j-center"
                        >
                            <image
                                style="
                                    width: 30rpx;
                                    height: 30rpx;
                                    margin-bottom: 10rpx;
                                "
                                src="@/static/image/checkbox-remind.png"
                            ></image>
                            <view class="right-text-yes">{{
                                title[type]
                            }}</view>
                            <view class="right-text-yes">已打卡</view>
                        </view>
                        <view v-else class="d-flex d-c a-center j-center">
                            <view v-if="moment(selectDate).format('YYYY-MM-DD')==moment().format('YYYY-MM-DD')" class="right-text-no">今日</view>
                            <view v-else class="right-text-no">当日</view>
							<view class="right-text-no">未打卡</view>
                        </view>
                    </view>
                </view>
                <template
                    v-if="isNotjoin(pageData)"
                >
                    <view class="not-joined">—— 以下胖友未加入打卡 ——</view>
                    <view
                        class="member-row d-flex a-center j-between"
                        v-for="(item,index) in pageData.filter((item:any) => !isNoAdd(item))"
                    >
                        <view class="row-divider" v-if="index > 0"></view>
                        <view class="row-left d-flex a-center">
                            <userhead
                                width="100"
                                height="100"
                                :saveKey="item.userId"
                                :isVip="item.isVip"
                                :src="item.userPhoto"
                            />
                            <view class="row-left-info d-flex a-center">
                                <view class="left-info-top d-flex a-center">
                                    <view style="padding-top: 5rpx">
                                        <sexage
                                            :sex="item.sex"
                                            :age="item.age"
                                        />
                                    </view>
                                    <view
                                        class="userName"
                                        :style="{
                                            color:
                                                item.isVip === 2
                                                    ? '#F19945'
                                                    : '#333333',
                                        }"
                                        >{{ item.userName }}</view
                                    >
                                </view>
                            </view>
                        </view>
                        <view class="row-right">
                            <view v-if="isClockin(item)"></view>
                            <view v-else class="d-flex d-c a-center j-center">
                                <view class="right-text-no">未加入</view>
                                <view class="right-text-no">打卡</view>
                            </view>
                        </view>
                    </view>
                </template>
            </scroll-view>
			
			<view v-if="isTip" class="bottom-tip d-flex a-center j-between">
				<view class="d-flex j-center" style="flex: 1;">
					<view class="bottom-tip-text" style="padding-left: 30rpx;">正在查看历史打卡，</view>
					<view @tap="goToday" class="bottom-tip-text" style="text-decoration: underline; ">切换到今天</view>
				</view>
				<image @tap="isTip = false" src="@/static/image/close-tip.png" style="width: 30rpx;height: 30rpx;"></image>
			</view>
			
            <view class="content-below">
                <view class="below-userinfo d-flex a-center j-between">
                    <view class="userinfo-mark">我</view>
                    <view class="row-left d-flex a-center">
                        <userhead
                            width="100"
                            height="100"
                            :saveKey="userInfo.id"
                            :isVip="userInfo.isVip"
                            :src="userInfo.photo"
                        />
                        <view class="row-left-info">
                            <view class="left-info-top d-flex a-center">
                                <view style="padding-top: 5rpx">
                                    <sexage
                                        :sex="userInfo.sex"
                                        :age="userInfo.age"
                                    />
                                </view>
                                <view
                                    class="userName"
                                    :style="{
                                        color:
                                            userInfo.isVip === 2
                                                ? '#F19945'
                                                : '#333333',
                                    }"
                                    >{{ userInfo.nickName }}</view
                                >
                            </view>
                            <view class="left-info-below d-flex a-center">
                                <view class="info-below-text">{{
                                    myClockinDate(userInfo.id, pageData)
                                }}</view>
                            </view>
                        </view>
                    </view>
                    <view class="row-right">
                        <template
                            v-if="
                                myClockinDate(userInfo.id, pageData).indexOf(
                                    '暂未加入打卡'
                                ) === -1
                            "
                        >
                            <template
                                v-if="isClockin(pageData.filter((item:any) => item.userId == userInfo.id)[0])"
                            >
                                <template v-if="type == 4">
                                    <tm-button
                                        _style="margin: 0;font-weight: 600;"
                                        :width="200"
                                        :height="100"
                                        :shadow="-1"
                                        round="5"
                                        :linear-color="['#64F1C5', '#29DBA3']"
                                        linear="left"
                                        :fontSize="32"
                                        fontColor="#ffffff"
                                        @tap="goClockin"
                                        label="继续打卡"
                                    ></tm-button>
                                </template>
                                <template v-else>
                                    <tm-button
                                        _style="
                                            margin: 0;
                                            flex-direction: column !important;
											font-weight: 600;
                                        "
                                        :width="200"
                                        :height="100"
                                        :shadow="-1"
                                        round="5"
                                        :linear-color="['#FFA24A', '#FF824F']"
                                        linear="left"
                                        :fontSize="32"
                                        fontColor="#ffffff"
                                        @tap="goClockinRecord(userInfo.id)"
                                        label=""
                                    >
										<view>
											<view class="right-btn-below">已打卡</view>
											<view class="right-btn-top">查看详情</view>
										</view>
                                    </tm-button>
                                </template>
                            </template>
                            <template v-else>
                                <tm-button
                                    _style="margin: 0;font-weight: 600;"
                                    :width="200"
                                    :height="100"
                                    :shadow="-1"
                                    round="5"
                                    :linear-color="['#64F1C5', '#29DBA3']"
                                    linear="left"
                                    :fontSize="32"
                                    fontColor="#ffffff"
                                    @tap="goClockin"
                                    label="去打卡"
                                ></tm-button>
                            </template>
                        </template>
                        <template v-else>
                            <tm-button
                                _style="margin: 0;font-weight: 600;"
                                :width="200"
                                :height="100"
                                :shadow="-1"
                                round="5"
                                :linear-color="['#64F1C5', '#29DBA3']"
                                linear="left"
                                :fontSize="32"
                                fontColor="#ffffff"
                                @tap="addClockin"
                                label="加入打卡"
                            ></tm-button>
                        </template>
                    </view>
                </view>
            </view>
        </view>
        <!-- 发起打开弹窗 -->
        <uni-popup ref="popupInitiateClockin">
            <view class="popup-content">
                <view class="popup-content-top">
                    <view class="content-top-background"></view>
                    <view style="position: relative">
                        <view class="content-top-title"
                            >发送打卡通知到群</view
                        >
                        <view class="content-top-content">
                            <view class="top-content-type">{{
                                title[type]
                            }}</view>
                            <view class="top-content-title">打卡通知</view>
                            <view class="top-content-describe"
                                >当前打卡进度&nbsp;{{
                                    total
                                }}/{{pageData.filter((item:any) => isSelect(item)).length}}</view
                            >
                        </view>
                        <view class="top-content-remind d-flex a-center">
                            <view
                                @tap="isRemind = !isRemind"
                                v-if="!isRemind"
                                class="content-remind-no"
                            ></view>
                            <image
                                @tap="isRemind = !isRemind"
                                v-else
                                style="width: 30rpx; height: 30rpx"
                                src="@/static/image/checkbox-remind.png"
                            ></image>
                            <view
                                @tap="isRemind = !isRemind"
                                class="content-remind-text"
                                >提醒未打卡成员</view
                            >
                        </view>
                    </view>
                </view>
                <tm-button
                    _style="margin: 20rpx auto 0 auto;font-weight: 600;"
                    :width="540"
                    :height="80"
                    :shadow="-1"
                    round="5"
                    :linear-color="['#64F1C5', '#29DBA3']"
                    linear="left"
                    :fontSize="32"
                    fontColor="#ffffff"
                    @tap="sendClockinToChat"
                    label="发送"
                ></tm-button>
                <tm-button
                    _style="margin: 20rpx auto 0 auto;font-weight: 600;"
                    :width="540"
                    :height="80"
                    :shadow="-1"
                    round="5"
                    :fontSize="32"
                    fontColor="#999999"
                    color="#CCCCCC"
                    outlined
                    @tap="popupInitiateClockin.close()"
                    label="取消"
                ></tm-button>
            </view>
        </uni-popup>
        <!-- 打卡统计概览 -->
        <uni-popup ref="popupCount" v-if="popupCountShow" @maskClick="popupCountShow=false">
            <view class="popup-count">
                <view class="count-content">
                    <view class="content-count d-flex j-between wrap">
                        <view class="count-cell">
                            <view class="count-cell-content d-flex j-center">
                                <view class="count-cell-number">{{
                                    userCount.ljdk
                                }}</view>
                                <view class="count-cell-unit">天</view>
                            </view>
                            <view
                                class="count-cell-title d-flex a-center j-center"
                            >
                                <image
                                    class="cell-title-icon"
                                    src="@/static/image/icon-ljdk.png"
                                ></image>
                                <view class="cell-title-text">累计打卡</view>
                            </view>
                        </view>
                        <view class="count-cell">
                            <view class="count-cell-content d-flex j-center">
                                <view class="count-cell-number">{{
                                    userCount.lxdk
                                }}</view>
                                <view class="count-cell-unit">天</view>
                            </view>
                            <view
                                class="count-cell-title d-flex a-center j-center"
                            >
                                <image
                                    class="cell-title-icon"
                                    src="@/static/image/icon-lxdk.png"
                                ></image>
                                <view class="cell-title-text">连续打卡</view>
                            </view>
                        </view>
                        <view class="count-cell">
                            <view class="count-cell-content d-flex j-center">
                                <view class="count-cell-number">{{
                                    userCount.index + 1
                                }}</view>
                            </view>
                            <view class="count-cell-title d-flex j-center">
                                <image
                                    class="cell-title-icon"
                                    src="@/static/image/icon-qnpm.png"
                                ></image>
                                <view class="cell-title-text">群内排名</view>
                            </view>
                        </view>
                        <view class="count-cell">
                            <view class="count-cell-content d-flex j-center">
                                <view class="count-cell-number">{{
                                    userCount.initWeight
                                }}</view>
                                <view class="count-cell-unit">kg</view>
                            </view>
                            <view class="count-cell-title d-flex j-center">
                                <image
                                    class="cell-title-icon"
                                    src="@/static/image/icon-cstz.png"
                                ></image>
                                <view class="cell-title-text">初始体重</view>
                            </view>
                        </view>
                        <view class="count-cell">
                            <view class="count-cell-content d-flex j-center">
                                <view class="count-cell-number">{{
                                    userCount.newWeight
                                }}</view>
                                <view class="count-cell-unit">kg</view>
                            </view>
                            <view class="count-cell-title d-flex j-center">
                                <image
                                    class="cell-title-icon"
                                    src="@/static/image/icon-dqtz.png"
                                ></image>
                                <view class="cell-title-text">当前体重</view>
                            </view>
                        </view>
                        <view class="count-cell">
                            <view class="count-cell-content d-flex j-center">
                                <view class="count-cell-number">{{
                                    userCount.yjz
                                }}</view>
                                <view class="count-cell-unit">kg</view>
                            </view>
                            <view class="count-cell-title d-flex j-center">
                                <image
                                    class="cell-title-icon"
                                    src="@/static/image/icon-yjz.png"
                                ></image>
                                <view class="cell-title-text">已减重</view>
                            </view>
                        </view>
                    </view>
                    <view class="count-btn d-flex a-center j-center">
                        <view class="btn-all d-flex">
                            <view @tap="goUserProgramme" class="btn-left" style="color: #1dc691"
                                >他的方案</view
                            >
                            <view
                                @tap="goClockinRecord(userCount.userId)"
                                class="btn-right"
                                style="color: #ffffff"
                                >打卡详情</view
                            >
                        </view>
                    </view>
                </view>
                <image
                    @tap="popupCountShow = false"
                    class="popup-close"
                    src="@/static/image/popup-close.png"
                ></image>
            </view>
        </uni-popup>
        <drinkWater
            ref="drinkWaterPopup"
            :groupId="groupId"
			:recordDate="selectDate"
            :totalWater="totalWater"
            @submitWater="refreshData"
        ></drinkWater>
        <weightRecord
            ref="weightRecordPopup"
            :groupId="groupId"
			:recordDate="selectDate"
            :initial-vlue="currentWeight"
            @onCloseWeight="refreshData"
        ></weightRecord>
        <quoteWater
            ref="quoteWaterPopup"
            :groupId="groupId"
			:recordDate="selectDate"
            @refreshData="refreshData"
            @openDrinkWater="openDrinkWater"
        ></quoteWater>
		<quoteFood
		    ref="quoteFoodPopup"
		    :groupId="groupId"
			:type="type"
			:recordDate="selectDate"
		    @refreshData="refreshData"
		    @onClose="goFoodClockin"
		></quoteFood>
    </view>
	<!--日期选择-->
	<dateTable
		ref="datePopup"
		:markDate="markDate"
		:initialDate="selectDate"
		color="#1DC691"
		minDate="2023-1-1"
		:zIndex="999"
		:maxDate="moment().format('YYYY-MM-DD')"
		@onGetDate="onGetDate"
		@changeMoon="onChangeMoon">
	</dateTable>
</template>

<script setup lang="tsx" name="clockinState">
import { ref,nextTick } from "vue";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { queryClockDetail, getUserDetail, getFoodRecord,getUserPlanList,getFoodList,getGroupFoodDate } from "@/api/api";
import userhead from "@/components/userhead.vue";
import sexage from "@/components/sexage.vue";
import moment from "moment";
import "moment/dist/locale/zh-cn";
import { userData as userDataStore } from "@/store/userData";
import BigNumber from "bignumber.js";
import drinkWater from "./components/drinkWater.vue";
import weightRecord from "./components/weightRecord.vue";
import quoteWater from "./components/quoteWater.vue";
import quoteFood from "./components/quoteFood.vue";
import { imStore } from '@/store/imStore';
import dateTable from "@/components/dateTable.vue";

const type = ref(0);
const groupId = ref();
const pageData: any = ref([]);
const total = ref(0); //打卡人总数
const userData: any = userDataStore();
const userInfo: any = ref({}); //当前用户信息
const totalWater = ref(0); //建议喝水量
const currentWeight = ref(0); //当前体重
const isUserRecord = ref(false); //该用户是否存在记录
const isAdmin = ref(false); //用户是否为管理员或群主
onLoad((options: any) => {
    groupId.value = options.groupId;
    type.value = options.type;
	selectDate.value = moment(options.recordDate,"YYYY-MM-DD")
	
	if(moment(selectDate.value).format("YYYY-MM-DD")==moment().format("YYYY-MM-DD")){
		isTip.value = false
	}else{
		isTip.value = true
	}
    //refreshData();
});
onShow(() => {
    refreshData();
});
//根据类型返回改用户是否选中
const isSelect = (item:any) =>{
	if(item.isJoinClock&&type.value>=0&&type.value<=4)return true;
	if(item.isJoinClock2&&type.value==5)return true;
	if(item.isJoinClock3&&type.value==6)return true;
	return false;
}
const sendCustomMessageToGroup = (payload) => {
    return new Promise((resolve, reject) => {
        try {
            let message = uni.$TUIKit.createCustomMessage({
                to: groupId.value,
                conversationType: "GROUP",
                payload,
            });

        const IM_STORE:any = imStore();
        // 优化写法
        const messageList = [...IM_STORE.messageList, message];
        IM_STORE.setMessageList(messageList)
            uni.$TUIKit
                .sendMessage(message)
                .then((res) => {
                    IM_STORE.messageList = IM_STORE.messageList.map((item: any) => {
                        if (item.ID === res.data.message.ID) {
                            return res.data.message;
                        }
                            return item;
                    });
                    resolve(res);
                })
                .catch((err) => {
                    resolve(err);
                });
        } catch (error) {
            resolve(error);
        }
    });
};

const sendClockinToChat = async () => {
    const obj = {
        title: `快来${title.value[type.value]}打卡吧~`,
        type: type.value,
        groupId:groupId.value,
        progress: `${total.value}/${
            pageData.value.filter((item: any) => isSelect(item)).length
        }`,
    };

    const payload = {
        data: "1",
        description: `${title.value[type.value]}打卡提醒`,
        extension: JSON.stringify(obj),
    };
    const res = await sendCustomMessageToGroup(payload);
    if(res.code===0){
        uni.showToast({
            icon: "none",
            title: "打卡提醒已发送~",
            duration: 1500,
        });
    }

    popupInitiateClockin.value.close()
};

//加载页面数据
const refreshData = () => {
    //total.value = 0;
    isAdmin.value = false;
	let param = {
		//userId:userData.id,
		groupId:groupId.value,
		type:Number(type.value)+1,
		userRecordDate:moment(selectDate.value).format("YYYY-MM-DD"),
	}
	//{ groupId: groupId.value,type:Number(type.value)+1 }
    queryClockDetail(param).then((res: any) => {
        if (res.success) {
            pageData.value = res.data;
			
            let myAdmin = pageData.value.filter(
                (item: any) =>
                    (item.role == "Admin" || item.role == "Owner") &&
                    item.userId == userData.id
            );
            if (myAdmin.length > 0) isAdmin.value = true;

            //计算累计打卡人数
            let userList = pageData.value.filter(
                (item: any) => item.isJoinClock == 1
            );
			let countTotal = 0;
            userList.forEach((item: any) => {
                //计算累计打卡人数
                if (type.value == 0) {
                    if (item.breakfast > 0) countTotal++;
                }
                if (type.value == 1) {
                    if (item.lunch > 0) countTotal++;
                }
                if (type.value == 2) {
                    if (item.dinner > 0) countTotal++;
                }
                if (type.value == 3) {
                    if (item.addFood > 0) countTotal++;
                }
                if (type.value == 4) {
                    if (item.water > 0) countTotal++;
                }
                if (type.value == 5) {
                    if (item.weight > 0) countTotal++;
                }
                if (type.value == 6) {
                    if (item.sports > 0) countTotal++;
                }
            });
			total.value = countTotal;
            getUserDetail({ id: userData.id }).then((ress: any) => {
                if (ress.success) {
                    userInfo.value = ress.data;

                    let userHealth = pageData.value.filter(
                        (item: any) => item.userId == userInfo.value.id
                    );
                    if (userHealth.length > 0) {
                        //当前体重
                        currentWeight.value = userHealth[0].initWeight;
                        if (userHealth[0].weight > 0)
                            currentWeight.value = userHealth[0].weight;
                        //每天需要饮水总量计算
                        totalWater.value = Math.round(
                            new BigNumber(currentWeight.value)
                                .times(new BigNumber(40))
                                .toNumber()
                        );
                    }
                }
            });
        }
    });
    //查询是否存在相应的记录
    refreshRecord();
};
//查询喝水记录
const refreshRecord = () => {
    let params = {
        userId: userData.id,
        recordDate: moment().format("YYYY-MM-DD"),
        type: Number(type.value) + 1,
    };
    getFoodRecord(params).then((res: any) => {
        if (res.success) {
            if (
                res.data.userHealthRecordDetails != null &&
                res.data.userHealthRecordDetails.length > 0
            ) {
                isUserRecord.value = true;
            }
        }
    });
};
//获取打卡时间描述
const clockinDate = (item: any) => {
    let describe = "";
    if (item.lxdk > 0) {
        describe = "连续打卡" + item.lxdk + "天";
    } else {
		if (item.userRecordDate == null) {
		    describe = "从未打卡";
		} else {
		    var pastDate = moment(item.userRecordDate, "YYYY-MM-DD"); // 你的过去日期
		    var today = moment(); // 今天的日期
		    var daysDifference = today.diff(pastDate, "days");
		
		    describe =
		        title.value[type.value] +
		        "已有" +
		        daysDifference +
		        "天未打卡";
		}
    }
    return describe;
};
//判断今日是否打卡
const isClockin = (item: any) => {
    if (type.value == 0 && item.breakfast > 0) return true;
    if (type.value == 1 && item.lunch > 0) return true;
    if (type.value == 2 && item.dinner > 0) return true;
    if (type.value == 3 && item.addFood > 0) return true;
    if (type.value == 4 && item.water > 0) return true;
    if (type.value == 5 && item.weight > 0) return true;
    if (type.value == 6 && item.sports > 0) return true;
    return false;
};
const goBack = () => {
    uni.navigateBack();
};
const title = ref(["早餐", "午餐", "晚餐", "加餐", "喝水", "体重", "运动"]);
//----------------------发送打卡-------------------------
const popupInitiateClockin = ref();
const showInitiateClockin = () => {
    isRemind.value = false;
    popupInitiateClockin.value.open("center");
};
const isRemind = ref(false);
const myClockinDate = (userId: string) => {
    let str = "";
    let myClockin = pageData.value.filter(
        (item: any) => isSelect(item) && item.userId == userId
    );
    if (myClockin.length > 0) {
        str = clockinDate(myClockin[0]);
    } else {
        str = "暂未加入打卡";
    }
    return str;
};
const addClockin = () => {
    uni.showToast({
        icon: "none",
        title: "请联系群主或管理员",
        duration: 1500,
    });
};


const preloadFoodData = () => {

    return new Promise((resolve, reject) => {
        getFoodList({userId:userData.id}).then((res:any) => {
            let collectList = res.data.filter((item:any) => item.code=="9999999")[0].childNode.filter((node:any) => node.note == 0);
            res.data.forEach((item:any) => {
                item?.childNode.forEach((items:any) => {
                    items.isCheck = false;//是否选中
                    items.isCollect = false;//是否收藏
                    let i = collectList.filter((node:any) => node.code == items.code);
                    if(i.length>0)items.isCollect = true;
                })

            })




            res.data.forEach((item:any) =>{
                if(item.code!=="9999999"){
                    item.childNode = [];
                }
            })


            uni.setStorageSync('FOODDATA',res.data)
            resolve(true);

        });

    })

	
}

//跳转食物打卡页面
const goClockin = async() => {
    if (type.value <= 3) {
        const obj = uni.getStorageSync('FOODDATA');
        if(!obj){
            await preloadFoodData();
        }
		showRecord();
		/*
        uni.navigateTo({
            url: `/pages/dietGroupMenu/dietGroupMenu?groupId=${groupId.value}&type=${type.value}&recordDate=${moment(selectDate.value).format("YYYY-MM-DD")}`,
        });
		*/
    }else{
		showRecord();
	}
	/*
    if (type.value == 4) {
        showDrinkWater();
    }
    if (type.value == 5) {
        showWeightRecord();
    }
    if (type.value == 6) {
        uni.navigateTo({
            url: `/pages/sportsGroupMenu/sportsGroupMenu?groupId=${groupId.value}&recordDate=${moment(selectDate.value).format("YYYY-MM-DD")}`,
        });
    }
	*/
};
//显示用户打卡统计
const popupCount = ref();
const userCount: any = ref();
const popupCountShow = ref(false);
const showPopupCount = (item: any, index: number) => {
	popupCountShow.value  = true;
	nextTick(()=>{
		item.index = index;
		userCount.value = item;
		popupCount.value.open("center");
	});
};

const quoteWaterPopup = ref();//显示喝水打卡
const quoteFoodPopup = ref();//显示食物打卡
//判断是否有记录，有测显示应用
const showRecord = () => {
    let is = isClockin(
        pageData.value.filter(
            (item: any) => item.userId == userInfo.value.id
        )[0]
    );

    let params = {
        userId: userData.id,
        recordDate: moment(selectDate.value).format("YYYY-MM-DD"),
        type: Number(type.value) + 1,
    };
    //查询当日是否有记录，有则选择是否引用
    getFoodRecord(params).then((res: any) => {
        let isList = false;
        if (res.success) {
            if (
                res.data.userHealthRecordDetails != null &&
                res.data.userHealthRecordDetails.length > 0
            ) {
                isList = true;
            }
        }
        if (isUserRecord.value && !is && isList && moment(selectDate.value).format("YYYY-MM-DD") == moment().format("YYYY-MM-DD")) {
            //quoteWaterPopup.value.open();
			if (type.value <= 3) {
			   quoteFoodPopup.value.open();
			}
			if (type.value == 4) {
			    quoteWaterPopup.value.open();
			}
        } else {
			if (type.value <= 3) {
				goFoodClockin();
			}
			if (type.value == 4) {
			    openDrinkWater();
			}
			if (type.value == 5) {
			    showWeightRecord();
			}
			if (type.value == 6) {
			    goSportsClockin();
			}
			
			/*
			if (type.value <= 3) {
			    uni.navigateTo({
			        url: `/pages/dietGroupMenu/dietGroupMenu?groupId=${groupId.value}&type=${type.value}&recordDate=${moment(selectDate.value).format("YYYY-MM-DD")}`,
			    });
			}
			if (type.value == 4) {
			    //showDrinkWater();
			}
			if (type.value == 5) {
			    showWeightRecord();
			}
			if (type.value == 6) {
			    uni.navigateTo({
			        url: `/pages/sportsGroupMenu/sportsGroupMenu?groupId=${groupId.value}&recordDate=${moment(selectDate.value).format("YYYY-MM-DD")}`,
			    });
			}
			*/
           // openDrinkWater();
        }
    });
};
const drinkWaterPopup = ref();
const openDrinkWater = () => {
    drinkWaterPopup.value.open();
};
//显示体重打卡
const weightRecordPopup = ref(); //体重控件
const showWeightRecord = () => {
    weightRecordPopup.value.open();
};
//跳转打卡记录
const goClockinRecord = (userId: string) => {
	popupCountShow.value=false;
    uni.navigateTo({
        url: `/pages/clockinRecord/clockinRecord?groupId=${groupId.value}&userId=${userId}`,
    });
};
//判断该用户是否加入打卡
const isNoAdd = (item:any) => {
	if(type.value<=4&&item.isJoinClock == 1)return true;
	if(type.value==5&&item.isJoinClock2 == 1)return true;
	if(type.value==6&&item.isJoinClock3 == 1)return true;
	return false;
}
const goUserProgramme = () => {
	getUserPlanList({userId:userCount.value.userId}).then((res:any) => {
		let programme =  res.data.list.filter((item:any) => item.inUse == 1);
		if(programme.length>0){
			uni.navigateTo({
			    url: `/pages/programmeDetails/programmeDetails?programmeId=${programme[0].id}&=&from=group`,
			});
		}else{
			uni.showToast({
				title:"对方暂无使用中的方案",
				icon:"none",
				duration:1500
			})
		}
	});
}
//判断是否有未加入打卡的人员
const isNotjoin = (data:any) =>{
	if(type.value>=0&&type.value<=4){
		let count =  data.filter((item:any) => item.isJoinClock === 0);
		if(count.length>0) return true;
		
	}
	if(type.value==5){
		let count =  data.filter((item:any) => item.isJoinClock2 === 0);
		console.log(count)
		if(count.length>0) return true;
		
	}
	if(type.value==6){
		let count =  data.filter((item:any) => item.isJoinClock3 === 0);
		if(count.length>0) return true;
		
	}
	return false;
}
//日期选择
const isTip = ref(false);
const selectDate:any = ref(new Date());//选中的日期
const markDate:any = ref([])//需要标识的日期
const datePopup = ref();
const onGetDate = (date:Date) => {
	selectDate.value = date;
	if(moment(selectDate.value).format("YYYY-MM-DD")==moment().format("YYYY-MM-DD")){
		isTip.value = false
	}else{
		isTip.value = true
	}
	refreshData();//重新加载数据
	//isAddDisable();//设置按钮是否可用
}
//日期选择
const showDateTable = () => {
	onChangeMoon(selectDate.value);//查询需要高亮显示的日期
	//console.log(datePopup.value)
	datePopup.value.open();
	
}
//日期选择器月份变更时操作
const onChangeMoon = (date:any) => {
	markDate.value = [];
	let param = {
		userId:userData.id,
		groupId:groupId.value,
		type:Number(type.value)+1,
		recordDate:moment(date).format("YYYY-MM-DD"),
	}
	getGroupFoodDate(param).then((res:any) => {
		res.data.forEach((item:any) => {
			markDate.value.push(item.recordDate)
		})
	})
}
//切换到今天
const goToday = () => {
	selectDate.value = moment();
	isTip.value = false;
	refreshData();//重新加载数据
}
//跳转至食物打卡记录
const goFoodClockin = () => {
	uni.navigateTo({
	    url: `/pages/dietGroupMenu/dietGroupMenu?groupId=${groupId.value}&type=${type.value}&recordDate=${moment(selectDate.value).format("YYYY-MM-DD")}`,
	});
}
//跳转至运动打卡
const goSportsClockin = () => {
	uni.navigateTo({
	    url: `/pages/sportsGroupMenu/sportsGroupMenu?groupId=${groupId.value}&recordDate=${moment(selectDate.value).format("YYYY-MM-DD")}`,
	});
}

</script>

<style lang="scss" scoped>
@import "./clockinState.scss";
</style>

